package com.hzw.core.store

import android.annotation.SuppressLint
import android.content.Context
import com.hzw.core.ctx.appCtx

object SpUtils {

    private val map: MutableMap<String, SP> = mutableMapOf()

    fun getInstance(name: String = "", mode: Int = Context.MODE_PRIVATE): SP {
        var spName = name
        if (spName.isBlank()) {
            spName = "sp_hzw"
        }
        val key = "$spName:$mode"
        var sp = map[key]
        if (sp == null) {
            synchronized(SpUtils::class.java) {
                sp = map[key]
                if (sp == null) {
                    sp = SP(name, mode)
                    map[key] = sp!!
                }
            }
        }
        return sp!!
    }
}

@SuppressLint("ApplySharedPref")
class SP(name: String, mode: Int) {

    private val sp = appCtx.getSharedPreferences(name, mode)

    fun put(key: String, value: String, isCommit: Boolean = false) {
        if (isCommit) {
            sp.edit().putString(key, value).commit()
        } else {
            sp.edit().putString(key, value).apply()
        }
    }

    fun getString(key: String, defaultValue: String = ""): String {
        return sp.getString(key, defaultValue)!!
    }

    fun put(key: String, value: Int, isCommit: Boolean = false) {
        if (isCommit) {
            sp!!.edit().putInt(key, value).commit()
        } else {
            sp!!.edit().putInt(key, value).apply()
        }
    }

    fun getInt(key: String, defaultValue: Int = -1): Int {
        return sp!!.getInt(key, defaultValue)
    }

    fun put(key: String, value: Long, isCommit: Boolean = false) {
        if (isCommit) {
            sp!!.edit().putLong(key, value).commit()
        } else {
            sp!!.edit().putLong(key, value).apply()
        }
    }

    fun getLong(key: String, defaultValue: Long = -1L): Long {
        return sp!!.getLong(key, defaultValue)
    }

    fun put(key: String, value: Float, isCommit: Boolean = false) {
        if (isCommit) {
            sp!!.edit().putFloat(key, value).commit()
        } else {
            sp!!.edit().putFloat(key, value).apply()
        }
    }

    fun getFloat(key: String, defaultValue: Float = -1f): Float {
        return sp!!.getFloat(key, defaultValue)
    }

    fun put(key: String, value: Boolean, isCommit: Boolean = false) {
        if (isCommit) {
            sp!!.edit().putBoolean(key, value).commit()
        } else {
            sp!!.edit().putBoolean(key, value).apply()
        }
    }

    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return sp!!.getBoolean(key, defaultValue)
    }

    fun put(
        key: String,
        value: Set<String>,
        isCommit: Boolean = false
    ) {
        if (isCommit) {
            sp!!.edit().putStringSet(key, value).commit()
        } else {
            sp!!.edit().putStringSet(key, value).apply()
        }
    }


    fun getStringSet(
        key: String,
        defaultValue: Set<String> = emptySet()
    ): Set<String> {
        return sp!!.getStringSet(key, defaultValue)!!
    }

    fun getAll(): Map<String, *>? {
        return sp!!.all
    }


    operator fun contains(key: String): Boolean {
        return sp!!.contains(key)
    }


    fun remove(key: String, isCommit: Boolean = false) {
        if (isCommit) {
            sp!!.edit().remove(key).commit()
        } else {
            sp!!.edit().remove(key).apply()
        }
    }

    fun clear(isCommit: Boolean = false) {
        if (isCommit) {
            sp!!.edit().clear().commit()
        } else {
            sp!!.edit().clear().apply()
        }
    }
}